using Function.SkillEditor.Runtime;
using SCore.Editor.WindowDraw;
using UnityEditor;
using UnityEngine;

namespace Function.SkillEditor.Editor
{
    public class SkillBuffClipDraw : ISkillClipDrawer
    {
        //private ReorderableList _reorderableList = new ReorderableList();
        private GenericMenu _genericMenu;

        public override void OnInspector(Rect rect)
        {
            base.OnInspector(rect);
            var clip = _skillClip as SkillBuffClip;

            //type
            var bPointRect = new Rect(rect.x, GetInspectorHeight(rect), rect.width, 20);
            GUI.Label(new Rect(bPointRect.x, bPointRect.y, bPointRect.width * 0.3f, bPointRect.height), "Buff类型");
            clip._skillBuffType = (SkillBuffType)EditorGUI.EnumPopup(
                new Rect(bPointRect.x + bPointRect.width * 0.3f, bPointRect.y, bPointRect.width * 0.7f,
                    bPointRect.height),
                clip._skillBuffType);
            var lableWidth = EditorGUIUtility.labelWidth;
            //count
            EditorGUIUtility.labelWidth = 60;
            clip._count = EditorGUI.IntSlider(new Rect(rect.x, GetInspectorHeight(rect), rect.width, 20), "检测次数",
                clip._count, 1, _skillClip._lengthFrame + 1);
            if (clip._count > _skillClip._lengthFrame + 1)
            {
                clip._count = _skillClip._lengthFrame + 1;
            }

            //trigger
            bPointRect = new Rect(rect.x, GetInspectorHeight(rect), rect.width, 20);
            EditorGUI.HelpBox(new Rect(rect.x, bPointRect.y, rect.width, 20 * (clip._triggers.Count + 1)), "触发器",
                MessageType.None);
            if (GUI.Button(
                    new Rect(bPointRect.x + bPointRect.width * 0.65f, bPointRect.y, bPointRect.width * 0.35f,
                        bPointRect.height), "+"))
            {
                GenericMenu genericMenu = new GenericMenu();
                genericMenu.AddItem(new GUIContent("None", "None"), false, null);
                _skillRunLine._skillTracks.ForEach(track =>
                {
                    if (track is SkillTriggerTrack)
                    {
                        var triggerTrack = track as SkillTriggerTrack;
                        triggerTrack.GetClips().ForEach(fclip =>
                        {
                            var triggerClip = fclip.As<SkillTriggerClip>();
                            genericMenu.AddItem(new GUIContent($"{triggerClip._startFrame} " +
                                                               $"{triggerClip._endFrame} "
                                                               + $"T:{triggerClip._skillTriggerType}S:{triggerClip._skillAttackShape}"),
                                false,
                                () =>
                                {
                                    if (!clip._triggers.Contains(triggerClip))
                                        clip._triggers.Add(triggerClip);
                                });
                        });
                    }
                });
                genericMenu.ShowAsContext();
            }

            for (int offest = clip._triggers.Count - 1; offest >= 0; offest--)
            {
                var trigger = clip._triggers[offest];
                bPointRect = new Rect(rect.x, GetInspectorHeight(rect), rect.width, 20);
                EditorGUI.ObjectField(new Rect(bPointRect.x, bPointRect.y,
                    bPointRect.width * 0.65f,
                    bPointRect.height), trigger, typeof(SkillTriggerClip), false);
                if (GUI.Button(
                        new Rect(bPointRect.x + bPointRect.width * 0.65f, bPointRect.y, bPointRect.width * 0.35f,
                            bPointRect.height), "-"))
                {
                    clip._triggers.RemoveAt(offest);
                }
            }

            EditorGUIUtility.labelWidth = lableWidth;
        }

        protected override void BgGUI()
        {
            base.BgGUI();
            var clip = _skillClip as SkillBuffClip;
            var triggersFrame = clip.GetTriggersFrame();
            float lengthFrame = _rect.width / clip._lengthFrame;
            foreach (var frame in triggersFrame)
            {
                var triggerFrame = frame - clip._startFrame;
                EditorGUI.DrawRect(new Rect(_rect.x + lengthFrame * triggerFrame, _rect.yMax - 3, 3, 3),
                    EditorDrawHelp._white);
            }
        }

        protected override void ClipNameGUI()
        {
            var clip = _skillClip as SkillBuffClip;
            EditorGUI.LabelField(_rect, $"T:{clip._skillBuffType},C:{clip._count}",
                EditorDrawHelp.GetTextGUIStyle(10, TextAnchor.MiddleCenter,
                    EditorDrawHelp._black, FontStyle.Bold));
        }
    }

    [TrackDrawEditor(typeof(SkillBuffTrack), typeof(SkillBuffClipDraw))]
    public class SkillBuffTrackDraw : ISkillTrackDrawer<SkillBuffTrack>
    {
        public SkillBuffTrackDraw(SkillBuffTrack skillTrack, TrackRegisterAttribute trackRegister) : base(skillTrack,
            trackRegister)
        {
        }

        protected override void DrawGroup()
        {
            EditorGUI.LabelField(new Rect(_headRect.x + 10, _headRect.y, _headRect.width, _headRect.height), "Buff");
        }
    }
}